package dataImport.action.helper;

import java.util.Set;
import java.util.TreeSet;
import dataImport.exception.UniquenessRestrictionViolatedException;


public class UnityGuardian {

	protected Set<String> uniqueKey;

	public UnityGuardian() {

		this.uniqueKey = new TreeSet<String>();
	}

	protected static final String createValue(final String... values) {
		if (values == null) {
			return "null";
		}

		String value = "";

		for (final String v : values) {
			value = value + ":" + v;
		}

		return values.length > 0 ? value.substring(1) : value;
	}

	public final void ensureUniqueness(final String... values) throws UniquenessRestrictionViolatedException {

		final String value = UnityGuardian.createValue(values);

		if (this.uniqueKey.contains(value)) {
			throw new UniquenessRestrictionViolatedException();
		}

		this.uniqueKey.add(value);
	}
}
